The Simple Way
If you research this topic you will see developers suggesting creating properties in your project, adding profile sections to populate those properties, and possibly even configuring the Maven Surefire plugin. While those approaches will work, there is a much simpler approach.
The Surefire plugin supports two properties called groups
and excludedGroups
to respectively include and exclude any JUnit 5, JUnit 4 and even TestNG tags. So to execute tests tagged with slow
or fast
you can run the following from the command line:
mvn test -Dgroups=fast,slow
This simply sets the groups
property from the command line. The Surefire plugin then uses it to select the tests it executes. It is not necessary to alter the pom.xml
file to run tagged tests. The use of profiles
is not necessary.
By the way; if there is nothing in that property, all tests are run.
Tagging Tests
Just a reminder, the tagging of tests is performed with annotations:
public
class
ClassATest
{
@Test
@Tag
(
"development"
)
@Tag
(
"production"
)
void
testCaseA(TestInfo testInfo) {
}
}
Again, this is a simple way to manage test runs with tags.